//
//  WelcomeViewController.swift
//  TYHWeibo
//
//  Created by IOS on 15/8/1.
//  Copyright © 2015年 IOS. All rights reserved.
//

import UIKit
import SDWebImage
class WelcomeViewController: UIViewController {

    //图像底部约束
    private var iconBottomCons : NSLayoutConstraint?
    
    override func viewDidLoad() {
        super.viewDidLoad()

        prepareUI()
        
        //加载头像
        if let urlString = UserAccount.sharedAccount?.avatar_large {
            iconView.sd_setImageWithURL(NSURL(string: urlString))
        }
        
    }
    
    override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(animated)
        
        //修改约束
        iconBottomCons?.constant = -(UIScreen.mainScreen().bounds.height + (iconBottomCons?.constant)!)
        
        //动画 usingSpringWithDamping:弹力系数 0 - 1之间,越小弹的越厉害, initialSpringVelocity: 初始速度
        UIView.animateWithDuration(1.2, delay: 0.0, usingSpringWithDamping: 0.8, initialSpringVelocity: 5.0, options: UIViewAnimationOptions(rawValue: 0), animations: { () -> Void in
                self.view.layoutIfNeeded()
            
            }) { (_) -> Void in
                //发送通知切换控制器
                NSNotificationCenter.defaultCenter().postNotificationName(TYHRootViewControllerSwitchNotification, object: true)
        }
    }
    
    private func prepareUI() {
        view.addSubview(backImageView)
        view.addSubview(iconView)
        view.addSubview(lable)
        
        //自动布局
        
        backImageView.ff_Fill(view)
//        backImageView.translatesAutoresizingMaskIntoConstraints = false
//        view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|-0-[subview]-0-|", options: NSLayoutFormatOptions(rawValue: 0), metrics: nil, views: ["subview": backImageView]))
//            view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|-0-[subview]-0-|", options: NSLayoutFormatOptions(rawValue: 0), metrics: nil, views: ["subview": backImageView]))
        //头像
        
        let cos = iconView.ff_AlignInner(type: ff_AlignType.BottomCenter, referView: view, size: CGSize(width: 90, height: 90), offset: CGPoint(x: 0, y: -160))
        //记录底边约束
        iconBottomCons = iconView.ff_Constraint(cos, attribute: NSLayoutAttribute.Bottom)
        
//        iconView.translatesAutoresizingMaskIntoConstraints = false
//        view.addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: view, attribute: NSLayoutAttribute.CenterX, multiplier: 1.0, constant: 0))
//        view.addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1.0, constant: 90))
//        view.addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute ,multiplier: 1.0, constant: 90))
//        view.addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem: view, attribute: NSLayoutAttribute.Bottom, multiplier: 1.0, constant: -160))
//        iconBottomCons = view.constraints.last
        
        //标签
        lable.ff_AlignVertical(type: ff_AlignType.BottomCenter, referView: iconView, size: nil, offset: CGPoint(x: 0, y: 16))
//        lable.translatesAutoresizingMaskIntoConstraints = false
//        view.addConstraint(NSLayoutConstraint(item: lable, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.CenterX, multiplier: 1.0, constant: 0))
//        view.addConstraint(NSLayoutConstraint(item: lable, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.Bottom, multiplier: 1.0, constant: 16))
    }
    
    private lazy var backImageView :UIImageView = UIImageView(image: UIImage(named: "ad_background"))
    private lazy var iconView: UIImageView = {
        let iv = UIImageView(image: UIImage(named: "avatar_default_big"))
        iv.layer.masksToBounds = true
        iv.layer.cornerRadius = 45
        return iv
    }()
    private lazy var lable: UILabel = {
        let lable = UILabel()
        lable.text = "欢迎归来"
        lable.sizeToFit()
        
        return lable
    }()

}
